﻿niggles.Register({
    Initialise: function (niggles) {
        var me = this;
        niggles._snippets.list.Execute = function () { return me.BuildList($(niggles._snippets.list)); };
    },
    BuildList: function (listSnippet) {
        var me = this;
        var list = $(listSnippet.html());
        list.AddRow = function (rowContent, tag) {
            var row = $('<tr class="list-item-selectable"></tr>');
            row.attr('tag', tag);
            $(rowContent).each(function () { row.append($('<td>' + this + '</td>')); });
            row.hover(
                function (target) { $(this).addClass('list-item-hover'); },
                function (target) { $(this).removeClass('list-item-hover'); }
            );
            row.click(function () { me._selectRow($(this), list); });
            this.find('tbody:last').append(row);
        }
        list.SetHeadings = function (headerContent) {
            var header = $('<tr></tr>');
            $(headerContent).each(function () { header.append($('<th>' + this + '</th>')); });
            var thead = this.find('thead');
            thead.children().remove();
            thead.append(header);
        }
        list.OnSelected = new Array();
        return list;
    },
    _selectRow: function ($row, $list) {
        if ($list.selectedRow != null) {
            $list.selectedRow.removeClass('list-item-selected');
        };
        $list.selectedRow = $row;
        $row.addClass('list-item-selected');
        $($list.OnSelected).each(function () { this($row, $list); });
    }
});